package pcjavanet.mobilesell.service;

import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import pcjavanet.mobilesell.model.QryCondition;
import pcjavanet.mobilesell.model.QryTerm;

public class ServiceUtil {
	private static String DATE_FORMAT_LONG="yyyy-MM-dd HH:mm:ss";
	
	private static String DATE_FORMAT_SHORT="yyyy-MM-dd";
	public static void main(String[] args){
		String str = "2012-09-12 12:40:44";
		SimpleDateFormat sdf=new SimpleDateFormat(DATE_FORMAT_LONG);
		try {
			sdf.parseObject(str);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public static  Object[] getParamValues(Type t, String value,boolean islike) {
		Object[] rtv = null;
		try {
			if (t == Double.class ||  t == double.class) {
				rtv = new Object[] { new Double(value) };
			}else if (t == Float.class ||  t == float.class) {
				rtv = new Object[] { new Float(value) };
			} else 	if (t == Long.class ||  t == long.class) {
				rtv = new Object[] { new Long(value) };
			} else 	if (t == Boolean.class ||  t == boolean.class) {
				rtv = new Object[] { new Boolean(value) };
			} else 	if (t == int.class ||  t == Integer.class) {
				rtv = new Object[] { new Integer(value) };
			} else if (t == String.class) {
				if ( islike )
					rtv = new Object[] { new String("%"+value+"%") };
				else 
					rtv = new Object[] { new String(value) };
			} else  if ( t == java.util.Date.class ) {
				
				Date date = null;
				if ( value.length() <19 && value.length() >=10 ){
					SimpleDateFormat sdf=new SimpleDateFormat(DATE_FORMAT_SHORT); 
					value = value.substring(0, 10);
					date = sdf.parse( value );
				}
				
				if ( value.length()   >=19 ){
					SimpleDateFormat sdf=new SimpleDateFormat(DATE_FORMAT_LONG); 
					value = value.substring(0, 19);
					date = sdf.parse( value );
				}
				
				rtv = new Object[] { date  };
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rtv;
	}

	public static void dealCriteria( QryCondition condition, Object c ) {
		boolean like = false;
		if (condition.getQryLikeEnable() != null && condition.getQryLikeEnable().equalsIgnoreCase("true")) {
			like = true;
		}
		Map<String, QryTerm> terms = condition.getQryMap();
		if ( terms.keySet() != null ) {
		java.util.Iterator<String> names = terms.keySet().iterator();
		while (names.hasNext()) {
			String name = names.next();
			QryTerm term = terms.get(name);
			System.out.println(name+";"+term.getName() +";"+term.getBeginValue()+";"+term.getEndValue() + ";"+term.getType());
			String type = term.getType();
			String beginValue = term.getBeginValue();
			String endValue = term.getEndValue();
			String criteriaMethodName = "and" + name;
			String criteriaMethodName2 = "and" + name;
			if (   !type.equals(QryTerm.DATE_TYPE) && ( beginValue == null || beginValue.equals("") ) ) {

			} else {
				if (type.equals(QryTerm.STRING_TYPE)) {
					if (like) {
						criteriaMethodName = criteriaMethodName + "Like";
					} else {
						criteriaMethodName = criteriaMethodName + "EqualTo";
					}
				}
				if (type.equals(QryTerm.INT_TYPE)) {
					criteriaMethodName = criteriaMethodName + "EqualTo";
				}
				if ( type.equals(QryTerm.DATE_TYPE)) {
					String beginMethod = criteriaMethodName
							+ "GreaterThanOrEqualTo";
					criteriaMethodName2 = criteriaMethodName
							+ "LessThanOrEqualTo";
					criteriaMethodName  =  beginMethod ;
				}
				try { 
					Method[] ms = c.getClass().getMethods() ; 
					for (int i = 0; i < ms.length; i++) {
						boolean breakFlag = false;
						if (beginValue != null && !"".equals(beginValue)  && ms[i].getName().equals(criteriaMethodName)  ) {
							Type t = ms[i].getGenericParameterTypes()[0];
							 
							ms[i].invoke(c, getParamValues( t, beginValue ,like));
							breakFlag  = true ; 
						}
						if (  endValue != null && !"".equals(endValue)  && ms[i].getName().equals(criteriaMethodName2)  ) {
							Type t = ms[i].getGenericParameterTypes()[0];
							ms[i].invoke(c, getParamValues( t, endValue ,like));
							breakFlag = true; 
						}
						if ( breakFlag  )
							break ;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		}
	}
}
